package cn.afactory.tygz.ui.base.views.search;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.adapter.BaseListAdapter;
import cn.afactory.tygz.ui.base.entity.LoganHotWordEntity;

public class HotSearchScrollView extends LinearLayout {
    private static final int DEFAULT_ITEM_COUNT = 4;
    private LinearLayout hot_keyword_container_panel;
    private GridView mHotKeywords;
    private KeywordAdapter mAdapter;
    private List<LoganHotWordEntity> mKeywordList = new ArrayList<LoganHotWordEntity>();
    private OnItemClickListener mListener;
    private int mItemMargin;

    public HotSearchScrollView(Context context) {
        super(context);
        initView();
    }

    public HotSearchScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public HotSearchScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View view = inflate(getContext(), R.layout.hot_search_scroll_view, this);
        mHotKeywords = (GridView) view.findViewById(R.id.hot_keyword_gv);
        mHotKeywords.setNumColumns(DEFAULT_ITEM_COUNT);
        mAdapter = new KeywordAdapter(getContext());
        mHotKeywords.setAdapter(mAdapter);

        mHotKeywords.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) {
                    mListener.OnItemClick(view, position, mAdapter.getItem(position));
                }
            }
        });
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setData(List<LoganHotWordEntity> data) {
        mKeywordList.clear();
        if (data != null) {
            mKeywordList.addAll(data);
        }
        mAdapter.addDataAll(data);
        mAdapter.notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener l) {
        mListener = l;
    }

    public interface OnItemClickListener {
        void OnItemClick(View v, int position, LoganHotWordEntity item);
    }

    class KeywordAdapter extends BaseListAdapter<LoganHotWordEntity, KeywordAdapter.ViewHolder> {
        public KeywordAdapter(Context ctx) {
            super(ctx);
        }

        @Override
        public View newView(int position, LayoutInflater inflater, ViewGroup parent) {
            View view = View.inflate(getContext(), R.layout.view_search_keyword_item_layout, null);
            return view;
        }

        @Override
        public ViewHolder onViewHolderCreate(int position, View view) {
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(int position, LoganHotWordEntity itemData, ViewHolder viewHolder) {
            viewHolder.hot_keyword_item.setText(itemData.content);
        }

        class ViewHolder extends BaseListAdapter.ViewHolder {
            TextView hot_keyword_item;

            public ViewHolder(View view) {
                hot_keyword_item = (TextView) view.findViewById(R.id.hot_keyword_item);
            }
        }
    }

}
